<template>
    <div class="container" v-cloak>
        <div class="blurBg" :style="{background:'url('+blurBg+') 0 0 no-repeat'}"></div>
        <div class="dataInfo">
            <div class="selectStudent font12" flex="main:justify cross:center">
                <input class="font12 title" placeholder="请输入精华标题" flex-box="1" maxlength="20" v-model="title">
                <div class="opacity5 font10 uploadNum" flex-box="0"><span v-text="title.length"></span>/20</div>
            </div>
            <div class="bottomLine"></div>
            <div class="inputModel">
                <div class="inputTitle Semibold font13">课堂重点内容</div>
                <textarea class="titleInput" ref="textarea" placeholder="请输入课堂重点内容" v-model="content"
                          maxlength="2000"></textarea>
                <div class="inputLimit"><span v-text="content.length"></span>/2000</div>
            </div>
            <div class="uploadModel">
                <div class="uploadTitle Semibold font13" flex="cross:bottom">
                    <div>课堂重点展示</div>
                    <div class="font10 uploadNum">(<span v-text="uploadList.length" :style="{color:uploadList.length>0?'#1dc6b4':''}"></span>/9)</div>
                </div>
                <ul class="uploadList">
                    <li class="uploadAdd" flex="main:center cross:center" v-if="uploadList.length<9"
                        @click="showAdd()">
                        <img src="../../images/addIcon.png" class="addIcon"/>
                    </li>
                    <li flex v-for="(list,index) in uploadList" class="picesWrap" :key="list"
                        @click="seePicesItem(index)">
                        <img :src="list.datathumbnail"/>
                        <img src="../../images/play.png" class="play" v-if="list.datatype==3"/>
                        <div class="removeIcon" @click.stop="removeItem(index)" v-if="canUploadClick"></div>
                    </li>
                </ul>
            </div>
            <div class="allSendBtn"  flex="main:center cross:center"
                 @click="send">发送
            </div>
        </div>
        <add-model ref="addModel" @addFuc="changeFile" :del="'audio'"></add-model>
        <div class="uploadWrapper" v-if="!canUploadClick"></div>
    </div>
</template>
<script type="text/babel">
    import API from '../../../api/api.js'
    import {domain} from '../../../api/api.js'
    import addModel from '../../../components/addModel.vue'
    const api = new API();
    export default{
        data(){
            return {
                title:new Date().Format('M月d日') +' 课堂回顾',
                content: '',
                uploadList: [],
                classId: this.$route.query.classId ? this.$route.query.classId : '',
                domain: domain,
                imgKeys: [],
                canUploadClick: true,
                showList: false,
                classJson: '',
                titleCheck:/[\ud800-\udbff][\udc00-\udfff]/
            }
        },
        mounted(){
            this.$util.jsBridgeCallBack('jsLeftAction', this.closeWebView);
            this.$nextTick(function () {
                setTimeout(()=>{
                    this.propsTitle();
                },300);
                document.documentElement.scrollTop=1;
                document.body.scrollTop=1;
            });
        },
        components: {
            addModel
        },
        methods: {
            closeWebView(){
                this.$util.JsBridge('closeWebView', {}, () => {
                })
            },
            showAdd(){
                this.$refs.addModel.addShow = true;
            },
            closeAdd(){
                this.$refs.addModel.addShow = false;
            },
            propsTitle(){
                this.$util.JsBridge('propsTitle', {
                    title: '课堂重点回顾',
                }, (res) => {
                })
            },
            seePicesItem(index){
                this.$util.JsBridge('seePicesItem', {
                    picItemIndex: index
                }, (res) => {
                })
            },
            removeItem(index){
                this.$delete(this.uploadList, index);
                this.$util.JsBridge('removePicesItem', {
                    picItemIndex: index
                }, (res) => {
                })
            },
            changeFile(type){
                //TODO:客户端上传文件
                if (9 - this.uploadList.length > 0) {
                    this.$util.JsBridge('getPicesList', {
                        picItemCount: 9 - this.uploadList.length,
                        showType: type,
                        appType: 'APP_CLASS_ESSENCE'
                    }, (res) => {
                        this.changeFileCallBack(res);
                        this.closeAdd();
                    })
                } else {
                    this.$toast('您最多只能上传9个附件');
                }
            },
            changeFileCallBack(res){
                this.imgKeys = res;
                for (let i = 0; i < res.length; i++) {
                    this.getImgInfo(res[i])
                }
            },
            getImgInfo(rs){
                this.$util.JsBridge('getImgInfo', {
                    fileKey: rs
                }, (res) => {
                    if (res.datathumbnail && res.datathumbnail.indexOf('data:image/png;base64') == -1) {
                        res.datathumbnail = 'data:image/png;base64,' + res.datathumbnail;
                    }
                    this.uploadList.push(res);
                })
            },
            check(){
                if(this.title===''){
                    this.$toast('请输入课堂回顾标题');
                    return false;
                }else if(this.titleCheck.test(this.title)){
                    this.$toast('标题内容不能包含表情');
                    return false;
                }else if(this.content===''){
                    this.$toast('请输入课堂回顾内容');
                    return false;
                }else if(this.uploadList.length===0){
                    this.$toast('请上传课堂回顾附件');
                    return false;
                }
                return true
            },
            send(){
                if (this.check()) {
                    if (this.canUploadClick) {
                        this.canUploadClick = false;
                        const content = this.$util.utf16toEntities(this.content).replace(/\n|\r\n/g, "<br>").replace(/[ ]/g, "&nbsp;");
                        let params = {
                            title: this.title,
                            content: content,
                            classId: this.classId,
                            appCode: 'APP_CLASS_ESSENCE'
                        };
                        let response = api.sendClassRoom(params);
                        response.then((res) => {
                            this.$toast(res.msg, true);
                            if (res.code === 200) {
                                this.$util.JsBridge('sendFinish', {
                                    taskTid: res.rs.taskTid,
                                    appType: 'APP_CLASS_ESSENCE',
                                    content: this.content,
                                    taskName: '课堂回顾',
                                    taskImageURL: this.domain + 'www/static/images/app_icon_v200/ketangjinghua-3x.png'
                                }, () => {});
                            } else {
                                this.canUploadClick = true;
                                this.$util.sendLog(1,'APP_CLASS_ESSENCE',JSON.stringify({time:new Date().Format('yyyy/MM/dd hh:mm'),name:'课堂回顾',code:'APP_CLASS_ESSENCE',path: '/app/classroom/add',api:'sendClassRoom',success:false,data:res}));
                            }
                        }).catch(()=>{
                            this.canUploadClick = true;
                            this.$util.sendLog(1,'APP_CLASS_ESSENCE',JSON.stringify({time:new Date().Format('yyyy/MM/dd hh:mm'),name:'课堂回顾',code:'APP_CLASS_ESSENCE',path: '/app/classroom/add',api:'sendClassRoom',success:false,data:'网络异常'}));
                        })
                    } else {
                        this.$toast('上传中，请稍后');
                    }
                }
            }
        }
    }
</script>
<style scoped lang="scss">
    @keyframes float {
        0% {
            font-size: 0.12rem;
            -webkit-transform: translate3d(0, 0, 0);
            transform: translate3d(0, 0, 0);
        }
        100% {
            font-size: 0.1rem;
            -webkit-transform: translate3d(0, -0.2rem, 0);
            transform: translate3d(0, -0.2rem, 0);
        }
    }

    @keyframes floatOut {
        0% {
            font-size: 0.1rem;
            -webkit-transform: translate3d(0, -0.2rem, 0);
            transform: translate3d(0, -0.2rem, 0);
        }
        100% {
            font-size: 0.12rem;
            -webkit-transform: translate3d(0, 0, 0);
            transform: translate3d(0, 0, 0);
        }
    }

    .container {
        width: 100%;
        height: 100%;
        background: #000;
        position: relative;
    }

    .bottomLine {
        opacity: 0.2;
        background: #fff;
        width: 3.5rem;
        height: 0.5px;
        margin: 0 auto;
    }
    .selectStudent {
        input {
            height: 100%;
            background: transparent;
            color: #fff;
        }
        padding: 0.28rem 0.12rem 0.1rem 0.12rem;
    }

    .inputModel {
        height: 1.83rem;
        width: 100%;
        padding-bottom: 0.13rem;
        border-bottom: 0.12rem solid rgba(0, 0, 0, 0.5);
    }

    .inputTitle {
        padding: 0.12rem 0 0.12rem 0.12rem;
    }

    .inputLimit {
        opacity: 0.4;
        font-size: 0.1rem;
        text-align: right;
        margin-right: 0.12rem;
    }

    .titleInput {
        width: 100%;
        padding: 0 0.12rem;
        background: transparent;
        font-size: 0.13rem;
        color: #ffffff;
        height: 1rem;
    }

    .uploadTitle {
        padding: 0.17rem 0 0.12rem 0;
    }

    .uploadAdd {
        border: 1px dashed #ccd2da;
        width: 0.8rem;
        height: 0.8rem;
    }

    .uploadModel {
        padding-left: 0.12rem;
    }

    .addIcon {
        width: 0.23rem;
        height: 0.23rem;
    }

    .picesWrap {
        background: #d8d8d8;
        position: relative;
        width: 0.8rem;
        height: 0.8rem;
        overflow: hidden;
        img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            -o-object-fit: cover;
        }
    }

    .uploadList {
        padding-bottom: 1rem;
        overflow: hidden;
        li {
            position: relative;
            margin-bottom: 0.1rem;
            margin-right: 0.1rem;
            float: left;
        }
    }

    .inputWrap {
        width: 2.7rem;
        height: 0.81rem;
        position: relative;
        background: rgba(250, 251, 255, .1);
        textArea {
            width: calc(100% - 0.24rem);
            height: 100%;
            background: transparent;
            padding: 0.08rem 0 0 0.08rem;
            font-size: 0.12rem;
            color: #fff;
        }
    }

    .removeIcon {
        width: 0.18rem;
        height: 0.18rem;
        position: absolute;
        right: 0.04rem;
        top: 0.04rem;
        background: url("../../images/delete.png") no-repeat center center/cover;
    }

    .uploadNum {
        opacity: 0.4;
        margin-left: 0.08rem;
    }

</style>
